package application;
	
import java.io.IOException;







import controlador.Login;
import controlador.Productos;
import controlador.Usuarios;
import controlador.Principal;
import controlador.Ventas;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;


public class Main extends Application {
	
	private Stage primaryStage;
	private static BorderPane rootLayout;
	private Principal controlleroot;
	private String usuario = null;
	
	public Main() {
		
	}
	
	@Override
	public void start(Stage primaryStage) {
		this.primaryStage = primaryStage;
		this.primaryStage.setTitle("Servicio PCMAX");
		
		try {
			FXMLLoader loader = new FXMLLoader(Main.class.getResource("Principal.fxml"));
			rootLayout = (BorderPane) loader.load();
			Scene scene = new Scene(rootLayout);
			primaryStage.setScene(scene);
			primaryStage.getIcons().add(new Image("Images/User.png"));
			primaryStage.setFullScreen(true);
			primaryStage.show();
			
			controlleroot = loader.getController();
			controlleroot.setMain(this);
			
		} catch(Exception e) {
			e.printStackTrace();
		}
		
		showLogin();
	}
	
	public Stage getPrimaryStage() {
		return primaryStage;
	}
	
	public void showProductos() {
		try {
			FXMLLoader loader = new FXMLLoader(Main.class.getResource("Productos.fxml"));
			AnchorPane overviewPage = (AnchorPane) loader.load();
			rootLayout.setCenter(overviewPage);
			
			Productos controller = loader.getController();
			controller.setMain(this);
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void showUsuarios() {
		try {
			FXMLLoader loader = new FXMLLoader(Main.class.getResource("Usuarios.fxml"));
			AnchorPane overviewPage = (AnchorPane) loader.load();
			rootLayout.setCenter(overviewPage);
			
			Usuarios controller = loader.getController();
			controller.setMain(this);
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void showVentas() {
		try {
			FXMLLoader loader = new FXMLLoader(Main.class.getResource("Ventas.fxml"));
			AnchorPane overviewPage = (AnchorPane) loader.load();
			rootLayout.setCenter(overviewPage);
			
			Ventas controller = loader.getController();
			controller.setMain(this);
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void showLogin() {
		try {
			FXMLLoader loader = new FXMLLoader(Main.class.getResource("Login.fxml"));
			AnchorPane page = (AnchorPane) loader.load();
			Stage dialogStage = new Stage();
			dialogStage.setTitle("Login");
			dialogStage.getIcons().add(new Image("Images/User.png"));
			dialogStage.initModality(Modality.WINDOW_MODAL);
			dialogStage.initOwner(primaryStage);
			Scene scene = new Scene(page);
			scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
			dialogStage.setScene(scene);
			
			Login controller = loader.getController();
			controller.setDialogStage(dialogStage);
			
			primaryStage.hide();
			dialogStage.showAndWait();
			
			usuario = controller.getUsuario();
			
			if (usuario != null){
				primaryStage.show();
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		launch(args);
	}
}
